﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ChanExt;
using C.Unite;
using C.Entities;

namespace ArtLand.OA.Project.Project
{
    public partial class Pro_RementChange : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
                FormInfo.ShowTriggerWin(Window1, txt_CONNUB, hid_A, "/PublicModel/SelectContat.aspx?s=1", "选择合同");
                LoadData();
            }
        }

        protected void LoadData()
        {
            if (!string.IsNullOrEmpty(Request["cid"]))
            {
                Proj_Contat con = TbsDbSession.Default.From<Proj_Contat>().Where(Proj_Contat._.ID == int.Parse(Request["cid"])).ToFirst();
                txt_CONNUB.Text = con.CONNUB;
                //txt_ARAMOT.Text = con.AMOUNT.Value.ToString();
                txt_CONNUB.Enabled = false;

            }


            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Proj_Rement ReMent = TbsDbSession.Default.From<Proj_Rement>().Where(Proj_Rement._.ID == int.Parse(Request["id"])).ToFirst();
                if (ReMent != null)
                {
                    txt_CONNUB.Text = ReMent.CONNUB;
                    txt_CONNUB.Enabled = false;
                    if (ReMent.ARAMOT.HasValue)
                        txt_ARAMOT.Text = ReMent.ARAMOT.Value.ToString();
                    if (ReMent.IAMOUT.HasValue)
                        txt_IAMOUT.Text = ReMent.IAMOUT.Value.ToString();
                    txt_INDATE.Text = ReMent.INDATE.Value.ToString("yyyy-MM-dd");
                    txt_INVNUB.Text = ReMent.INVNUB;
                    if (ReMent.PAMOUT.HasValue)
                        txt_PAMOUT.Text = ReMent.PAMOUT.Value.ToString();
                    txt_PERSON.Text = ReMent.PERSON;
                    txt_REDATE.Text = ReMent.REDATE.Value.ToString("yyyy-MM-dd");
                    txt_REMARK.Text = ReMent.REMARK;
                    txt_CONNAME.Text = ReMent.CONNAME;
                }
            }
            LoadConData();
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            Proj_Rement ReMent = new Proj_Rement();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                ReMent.ID = int.Parse(Request["id"]);
                ReMent.Attach();
                ReMent.ARAMOT = Convert.ToDecimal(txt_ARAMOT.Text);
                ReMent.CONNUB = txt_CONNUB.Text;
                ReMent.IAMOUT = Convert.ToDecimal(txt_IAMOUT.Text);
                ReMent.INDATE = Convert.ToDateTime(txt_INDATE.Text);
                ReMent.INVNUB = txt_INVNUB.Text;
                ReMent.PAMOUT = Convert.ToDecimal(txt_PAMOUT.Text);
                ReMent.PERSON = txt_PERSON.Text;
                ReMent.REDATE = Convert.ToDateTime(txt_REDATE.Text);
                ReMent.REMARK = txt_REMARK.Text;
                ReMent.REMARK = txt_REMARK.Text;
                ReMent.CONNAME = txt_CONNAME.Text;

                returnvalue = TbsDbSession.Default.Update<Proj_Rement>(ReMent);
            }
            else
            {
                ReMent.DeAttach();
                ReMent.ARAMOT = Convert.ToDecimal(txt_ARAMOT.Text);
                ReMent.CONNUB = txt_CONNUB.Text;
                ReMent.IAMOUT = Convert.ToDecimal(txt_IAMOUT.Text);
                ReMent.INDATE = Convert.ToDateTime(txt_INDATE.Text);
                ReMent.INVNUB = txt_INVNUB.Text;
                ReMent.PAMOUT = Convert.ToDecimal(txt_PAMOUT.Text);
                ReMent.PERSON = txt_PERSON.Text;
                ReMent.REDATE = Convert.ToDateTime(txt_REDATE.Text);
                ReMent.REMARK = txt_REMARK.Text;
                ReMent.Adder = CookInfo.EAdminTrueName();
                ReMent.AddID = CookInfo.EAdminid();
                ReMent.AddTime = DateTime.Now;
                ReMent.CONNAME = txt_CONNAME.Text;
                returnvalue = TbsDbSession.Default.Insert<Proj_Rement>(ReMent);
            }
            if (returnvalue > 0)
            {
                Alert.Show("更新成功！", "提示", ActiveWindow.GetHideRefreshReference());
            }
        }


        protected void LoadConData()
        {
            if (txt_CONNUB.Text != "")
            {
                Proj_Contat pc = TbsDbSession.Default.From<Proj_Contat>().Where(Proj_Contat._.CONNUB == txt_CONNUB.Text).ToFirst();
                if (pc != null)
                {
                    string ob = TbsDbSession.Default.From<Proj_Rement>().Where(Proj_Rement._.CONNUB == txt_CONNUB.Text).Select(Proj_Rement._.PAMOUT.Sum()).ToScalar().ToString();
                    decimal aom = 0;
                    if (ob != "")
                        aom = decimal.Parse(ob.ToString());
                    if (pc.AMOUNT.HasValue)
                        txt_ARAMOT.Text = pc.AMOUNT.Value.ToString();
                    Label14.Text = string.Format("<b>合同的总金额为：{0}</b>", pc.AMOUNT.Value);
                    if (aom > 0)
                    {
                        Label14.Text += string.Format("<br><br><span style='color:red;font-weight:bold;'>已收款：{0}</span>", aom.ToString());
                    }
                    decimal sy = pc.AMOUNT.Value - aom;
                    Label14.Text += string.Format("<br><br><span style='color:blue;font-weight:bold;'>剩余收款：{0}</span>", sy.ToString());
                }
            }
        }

        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            LoadConData();
        }

    }
}